package hello;

import org.hibernate.FlushMode;
import org.hibernate.Session;

import persistence.HibernateUtil;

/**
 * Chapter11 Conversation
 * @author Wei Sun
 *
 */
public class HelloWorldConversation {
	
	public static void main(String[] args) throws Exception {
		//testConversationSaveInTx();
	    //testConversationPersistOutTx();
	    
	    //testConversationSaveOutTx();
	    testConversationPersistInTx();
	}
	
	/**
	 * Example in book
	 * @throws Exception
	 */
	public static void testConversationSaveInTx() throws Exception{
		
		Session s2=HibernateUtil.getSessionFactory().openSession();
		s2.setFlushMode(FlushMode.MANUAL);//Extend the session
		//first request
		s2.beginTransaction();
		Message2 loadm=(Message2)s2.get(Message2.class, 1L);
		out(loadm.getText());
		s2.getTransaction().commit();
		
		//user thinking time...
		
		//second request
		s2.beginTransaction();
		Message2 nm=new Message2();
		nm.setText("second request");
		
		//native(identity): record is inserted. Wrong
		//increment:no record. Right
		Long id=(Long)s2.save(nm);
		out(id);
		s2.getTransaction().commit();
		
		//do not flush,roll back the conversation
		s2.close();
				
	}
	
	/**
	 * my test
	 * @throws Exception
	 */
	public static void testConversationSaveOutTx() throws Exception{
        
        Session s2=HibernateUtil.getSessionFactory().openSession();
        s2.setFlushMode(FlushMode.MANUAL);//Extend the session
        //first request
        s2.beginTransaction();
        Message2 loadm=(Message2)s2.get(Message2.class, 1L);
        out(loadm.getText());
        s2.getTransaction().commit();
        
        //user thinking time...
        
        //second request
        Message2 nm=new Message2();
        nm.setText("second request");
        
        //native(identity): no record. insert issued but roll back. Right
        //increment:no record. Right
        Long id=(Long)s2.save(nm);
        out(id);
        
        //do not flush,roll back the conversation
        s2.close();
                
    }
	
	/**
	 * Example in book
	 * @throws Exception
	 */
	public static void testConversationPersistOutTx() throws Exception{
        
        Session s2=HibernateUtil.getSessionFactory().openSession();
        s2.setFlushMode(FlushMode.MANUAL);//Extend the session
        //first request
        s2.beginTransaction();
        Message2 loadm=(Message2)s2.get(Message2.class, 1L);
        out(loadm.getText());
        s2.getTransaction().commit();
        
        //user thinking time...
        
        //second request
        Message2 nm=new Message2();
        nm.setText("second request");
        
        //native(identity): no record. Right
        //increment:no record. Right
        s2.persist(nm);
        
        //do not flush,roll back the conversation
        s2.close();
                
    }
	
	/**
	 * my test
	 * @throws Exception
	 */
	public static void testConversationPersistInTx() throws Exception{
        
        Session s2=HibernateUtil.getSessionFactory().openSession();
        s2.setFlushMode(FlushMode.MANUAL);//Extend the session
        //first request
        s2.beginTransaction();
        Message2 loadm=(Message2)s2.get(Message2.class, 1L);
        out(loadm.getText());
        s2.getTransaction().commit();
        
        //user thinking time...
        
        //second request
        s2.beginTransaction();
        Message2 nm=new Message2();
        nm.setText("second request");
        
        //native(identity): record is inserted. Wrong
        //increment:no record. Right
        s2.persist(nm);
        s2.getTransaction().commit();
        
        //do not flush,roll back the conversation
        s2.close();
                
    }

	public static void getParent(Class clazz) {
		Class parent = clazz.getSuperclass();
		if (parent != null) {
			System.out.println("###" + parent.getName());
			getParent(parent);
		}
	}

	static void out(Object obj) {
		System.out.println("--->" + obj);
	}
}
